 var polluteList=[];
 var ismove=true;
 var marginLeft=0;
 var count = 0;
// 外部调用初始化控件方法
function initPollute(url,data,callBackFunc){
	initPolluteData(url,data, callBackFunc);
}
function initSinglePollute(url,data,callBackFunc){
	initSinglePollute(url,data, callBackFunc);
}
//因子设备分组
function initPolluteGroup(url,data,callBackFunc){
	initPolluteGroupData(url,data, callBackFunc);
}
//因子分组
function initNewPolluteGroup(url,data,callBackFunc){
	initNewPolluteGroupData(url,data, callBackFunc);
}

function initPolluteForMultiStations(url,data,callBackFunc){
	initPolluteDataForMultiStations(url,data, callBackFunc);
}

// 外部调用获取参数方法
function getPolluteQueryParam() {
	var queryParams = {};
	var polluteCodes='';
	$("#polluteCodeDiv span").each(
		function(index, el) {
			if ($(this).hasClass('active')) {
				polluteCodes = polluteCodes === '' ? $(this).attr('id'): polluteCodes + ',' + $(this).attr('id');
				if(!isEmpty($(this).attr('polluteclass'))){
					polluteCodes = polluteCodes === '' ? $(this).attr('id')+'-Vt': polluteCodes + ',' + $(this).attr('id')+'-Vt';
				}
			}
	});
	queryParams.polluteCode = polluteCodes;
	return queryParams;
}
//外部调用获取参数方法
function getPolluteGroupQueryParam() {
	var queryParams = {};
	var polluteCodes='';
	for(i=0;i<polluteList.length;i++){
		polluteCodes = polluteCodes === '' ? polluteList[i] : polluteCodes+ ',' +polluteList[i];
	}
	queryParams.polluteCode = polluteCodes;
	return queryParams;
}

function initSinglePolluteHtml(rows,callBackFunc) {	
	var htmlVal = '<div class="factor"><div class="box-zsgc">';
	htmlVal += '<span class="fl marrt20" style="margin:3px 5px 4px 10px;"><b>因&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;子：</b></span>';
	htmlVal += '<div class="factors-div">';
	htmlVal += '<div id="polluteCodeDiv">';
	for(var i=0;i<rows.length;i++){
		if(i==0){
			htmlVal += '<span class="fl air-btn active factorbtn" polluteClass="'+rows[i].polluteClass+'" pollconCunit="'+rows[i].pollconCunit+'" id="'+rows[i].polluteCode+'" >'+rows[i].polluteName+'</span>';
		}else{
			htmlVal += '<span class="fl air-btn factorbtn" polluteClass="'+rows[i].polluteClass+'" pollconCunit="'+rows[i].pollconCunit+'" id="'+rows[i].polluteCode+'" >'+rows[i].polluteName+'</span>';
		}		
	}
	htmlVal += '</div></div></div>';	
	htmlVal += '<div class="sl-ext">';
	htmlVal += '<a class="n-cmbtn pd-jx btn-gray-border sl-e-more text-lf">展开<i></i></a>';
	htmlVal += '</div></div>'; 
    $("#factor").html(htmlVal);
    $(".factorbtn").bind("click",function(){
    	checkSingle($(this).attr("id"),callBackFunc);
    });
    $(".sl-ext").hide();
	$(".sl-ext").bind('click', function(event) {
		if ($(".sl-e-more").hasClass("opened")) {
			$(".sl-e-more").html("展开<i></i>");
			$(".sl-e-more").removeClass("opened");
			$(".box-zsgc").css("height","37px");
		} else {				
			$(".sl-e-more").html("收起<i></i>");
			$(".sl-e-more").addClass("opened");
			$(".box-zsgc").css("height","auto");
		}
	});
}
function initPolluteHtml(rows,callBackFunc) {	
	var htmlVal = '<div class="factor"><div class="box-zsgc">';
	htmlVal += '<span class="fl marrt20" style="margin:3px 5px 4px 10px;"><b>因&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;子：</b></span>';
	htmlVal += '<div class="factors-div">';
	htmlVal += '<span id="selAll" class="fl air-btn active" value="-1"">全选</span>';
	htmlVal += '<div id="polluteCodeDiv">';
	for(var i=0;i<rows.length;i++){
		htmlVal += '<span class="fl air-btn active factorbtn" polluteClass="'+rows[i].polluteClass+'" id="'+rows[i].polluteCode+'" >'+rows[i].polluteName+'</span>';
	}
	htmlVal += '</div></div></div>';	
	htmlVal += '<div class="sl-ext">';
	htmlVal += '<a class="n-cmbtn pd-jx btn-gray-border sl-e-more text-lf">展开<i></i></a>';
	htmlVal += '</div></div>'; 
    $("#factor").html(htmlVal);
    $(".factorbtn").bind("click",function(){
    	check($(this).attr("id"),callBackFunc);
    });
    $(".sl-ext").hide();
	$(".sl-ext").bind('click', function(event) {
		if ($(".sl-e-more").hasClass("opened")) {
			$(".sl-e-more").html("展开<i></i>");
			$(".sl-e-more").removeClass("opened");
			$(".box-zsgc").css("height","37px");
		} else {				
			$(".sl-e-more").html("收起<i></i>");
			$(".sl-e-more").addClass("opened");
			$(".box-zsgc").css("height","auto");
		}
	});
	$("#selAll").bind("click",function(){
		checkAll($(this),callBackFunc);
	});
}
function initPolluteGroupHtml(rows,callBackFunc) {	
	var htmlVal = '<div id="J_selectorSenior" class="J_selectorLine s-line s-senior" style="background-color:#fff;">';
	htmlVal += '<div class="sl-wrap"><div class="sl-key"><span><b>因子分类：</b></span></div>';
	htmlVal += '<div class="sl-value"><div class="sl-v-tab"><div class="sl-tab-trigger fl clearfix">';
	for(var i=0;i<rows.length;i++){
		for(var j=0;j<rows[i].equipLibList.length;j++){
			var pollute=rows[i].equipLibList[j];
			if($.inArray(pollute.polluteCode, polluteList)===-1){
				polluteList.push(pollute.polluteCode);
			}
		}
		htmlVal += '<span class="trig-item active" ><span class="text" id="'+rows[i].eid+'" >'+rows[i].equipName+'</span><i class="arrow"></i></span>';
	}
	htmlVal += '</div><div class="menu-move fr" style="display: block;">';
	htmlVal += '<div class="left-move fl"><span></span></div>';
	htmlVal += '<div class="right-move fl"><span></span></div></div></div></div>';

    $("#factor").html(htmlVal);
    var left=$(".sl-tab-trigger").css("margin-left").replace("px",""); 
    marginLeft=parseFloat(left);	
    $(".sl-ext").hide();
	$(".sl-ext").bind('click', function(event) {
		if ($(".sl-e-more").hasClass("opened")) {
			$(".sl-e-more").html("展开<i></i>");
			$(".sl-e-more").removeClass("opened");
			$(".box-zsgc").css("height","37px");
		} else {				
			$(".sl-e-more").html("收起<i></i>");
			$(".sl-e-more").addClass("opened");
			$(".box-zsgc").css("height","auto");
		}
	});
	$(".left-move").bind("click",function(){
		if(ismove){
			ismove=false;		
			if(marginLeft<0){
				if(marginLeft<=-100){
					marginLeft=marginLeft+100;
				}else{
					marginLeft=0;
				}
				$(".sl-tab-trigger").animate({
					 "margin-left":  marginLeft
		         }, 500);
			}
			ismove=true;
		}			
	});
	$(".right-move").bind("click",function(){	
		if(ismove){
			ismove=false;
			var width=$(".sl-v-tab").width();
			var allwidth=$(".sl-tab-trigger").width();
			if(marginLeft<=0){
				if(width-marginLeft<allwidth){
					marginLeft=marginLeft-100;
				}
				$(".sl-tab-trigger").animate({
					 "margin-left":  marginLeft
		         }, 500);
			}
		}
		ismove=true;
	});
	$(".trig-item").hover(function() {
		var eid=$(this).find("span").attr("id");
		var html="";
		$(".trig-item").removeClass("trig-curr");
		$(this).addClass("trig-curr");
		$(".pollutelist").remove();
		for(var i=0;i<rows.length;i++){
			if(rows[i].eid===parseInt(eid)){
				html ='<div class="sl-tab-cont pollutelist" style="display:none"><div class="sl-tab-cont-item commonscrollbar" style="display:block"><div class="sl-v-list"><div class="fl selAll">全选</div><ul class="J_valueList clearfix">';			
				for(var j=0;j<rows[i].equipLibList.length;j++){
					var pollute=rows[i].equipLibList[j];
					if($.inArray(pollute.polluteCode, polluteList)!==-1){
						html +='<li><a class="air-btn factorbtn active" rel="nofollow" polluteClass="'+rows[i].polluteClass+'" id="'+pollute.polluteCode+'" ><i></i>'+pollute.polluteName+'</a></li>';
					}else{
						html +='<li><a class="air-btn factorbtn" rel="nofollow" polluteClass="'+rows[i].polluteClass+'" id="'+pollute.polluteCode+'" ><i></i>'+pollute.polluteName+'</a></li>';
					}
					
				}
				html +='</ul></div></div></div>';
				$("#factor").append(html);
				$(".pollutelist").show();
				if($(".factorbtn").length==$(".factorbtn.active").length){
					$(".selAll").addClass("active");
				}
				if($(".factorbtn.active").length===0){
					$(".selAll").removeClass("active");
				}
				if($(".sl-tab-cont-item").height()>400){
					$(".sl-tab-cont-item").height(400);
				}
				$(".sl-tab-cont-item").width($(".sl-wrap").width()-150);
				$(".factorbtn").bind("click",function(){
					checkGroup($(this).attr("id"),callBackFunc);
			    });
				$(".selAll").bind("click",function(){
					checkAllByGroup($(this),callBackFunc);
				});
				break;
			}
		}
	}, function() {
		$(".pollutelist").hide(); 
		$(this).removeClass("trig-curr");
		$(".pollutelist").hover(function() {
    		$(".pollutelist").show(); 
    		$(this).addClass("trig-curr");
    	}, function() {
    		$(".pollutelist").hide(); 
    		$(this).removeClass("trig-curr");
    	}); 
	});
}
function initNewPolluteGroupHtml(rows,callBackFunc) {	
	var htmlVal = '<div class="factor"><div class="box-zsgc">';
	htmlVal += '<span class="fl marrt20" style="margin:3px 5px 4px 10px;"><b>因子类别：</b></span>';
	htmlVal += '<div class="factors-div">';
	htmlVal += '<span id="selAll" class="fl air-btn active" value="-1"">全选</span>';
	htmlVal += '<div id="polluteCodeDiv">';
	for(var i=0;i<rows.length;i++){
		htmlVal += '<span class="fl air-btn active factorbtn" id="'+rows[i].groupCode+'" >'+rows[i].groupName+'</span>';
	}
	htmlVal += '</div></div></div>';	
	htmlVal += '<div class="sl-ext">';
	htmlVal += '<a class="n-cmbtn pd-jx btn-gray-border sl-e-more text-lf">展开<i></i></a>';
	htmlVal += '</div></div>'; 
    $("#factor").html(htmlVal);
    $(".factorbtn").bind("click",function(){
    	check($(this).attr("id"),callBackFunc);
    });
    $(".sl-ext").hide();
	$(".sl-ext").bind('click', function(event) {
		if ($(".sl-e-more").hasClass("opened")) {
			$(".sl-e-more").html("展开<i></i>");
			$(".sl-e-more").removeClass("opened");
			$(".box-zsgc").css("height","37px");
		} else {				
			$(".sl-e-more").html("收起<i></i>");
			$(".sl-e-more").addClass("opened");
			$(".box-zsgc").css("height","auto");
		}
	});
	$("#selAll").bind("click",function(){
		checkAll($(this),callBackFunc);
	});
}
function checkSingle(id,callBackFunc) {
	$(".factorbtn").removeClass('active');
	$("#" + id).addClass('active');
	if (callBackFunc != undefined && callBackFunc != null){
		callBackFunc();
	}
}
function check(id,callBackFunc) {
	if ($("#" + id).hasClass('active')) {
		$("#" + id).removeClass('active');
		if($(".factorbtn.active").length !== $(".factorbtn").length){
			$("#selAll").removeClass('active');
		}
	} else {
		$("#" + id).addClass('active');
		if($(".factorbtn.active").length === $(".factorbtn").length){
			$("#selAll").addClass('active');
		}
	}
	if (callBackFunc != undefined && callBackFunc != null){
		callBackFunc();
	}
}
function checkAll(obj,callBackFunc) {
	if ($(obj).hasClass('active')) {
		$(obj).removeClass('active');
	} else {
		$(obj).addClass('active');
	}
	var a = '';
	$("#polluteCodeDiv span").each(function(index, el) {
		if ($(obj).hasClass('active')) {
			$(this).addClass('active');
		} else {
			$(this).removeClass('active');
		}
	});
	if (callBackFunc != undefined && callBackFunc != null){
		callBackFunc();
	}
}
function checkGroup(id,callBackFunc) {
	if ($("#" + id).hasClass('active')) {
		$("#" + id).removeClass('active');
		var index=$.inArray(id, polluteList);
		if(index!==-1){
			polluteList.splice(index, 1);
		}
		if($(".factorbtn.active").length !== $(".factorbtn").length){
			$("#selAll").removeClass('active');
		}
	} else {
		$("#" + id).addClass('active');
		if($.inArray(id, polluteList)===-1){
			polluteList.push(id);
		}
		if($(".factorbtn.active").length === $(".factorbtn").length){
			$("#selAll").addClass('active');
		}
	}
	if($(".factorbtn.active").length !==0){
		$(".trig-item.trig-curr").addClass("active");
	}
	if (callBackFunc != undefined && callBackFunc != null){
		callBackFunc();
	}
}
function checkAllByGroup(obj,callBackFunc) {
	if ($(obj).hasClass('active')) {
		$(obj).removeClass('active');
	} else {
		$(obj).addClass('active');
	}
	var a = '';
	$(".factorbtn").each(function(index, el) {
		if ($(obj).hasClass('active')) {
			$(this).addClass('active');
			if($.inArray($(this).attr("id"), polluteList)===-1){
				polluteList.push($(this).attr("id"));
			}	
		} else {
			$(this).removeClass('active');
			var index=$.inArray($(this).attr("id"), polluteList);
			if(index!==-1){
				polluteList.splice(index, 1);
			}	
		}
	});
	if (callBackFunc != undefined && callBackFunc != null){
		callBackFunc();
	}
}
function initSinglePollute(url,data, callBackFunc) {	
		ajaxRequest(url, null, data, null, function(resp) {
			initSinglePolluteHtml(resp.rows,callBackFunc);
			if (resp.rows.length >0) {
				$(".box-zsgc").css("height","auto");
				$(".factor").css("width",$(".n-dttitle").width()-20);
				
				$(".sl-e-more").html("展开<i></i>");
				$(".sl-e-more").removeClass("opened");
				if($(".box-zsgc").height()>37){
					$(".box-zsgc").css("height","37px");
					$(".sl-ext").show();
				}else{
					$(".sl-ext").hide();
				}						
			}
			if (callBackFunc != undefined && callBackFunc != null){
				callBackFunc();
			}
		}, null);	
}
function initPolluteData(url,data, callBackFunc) {	
	ajaxRequest(url, null, data, null, function(resp) {
		initPolluteHtml(resp.rows,callBackFunc);
		if (resp.rows.length >0) {
			$(".box-zsgc").css("height","auto");
			$(".factor").css("width",$(".n-dttitle").width()-20);
			
			$(".sl-e-more").html("展开<i></i>");
			$(".sl-e-more").removeClass("opened");
			if($(".box-zsgc").height()>37){
				$(".box-zsgc").css("height","37px");
				$(".sl-ext").show();
			}else{
				$(".sl-ext").hide();
			}						
		}
		if (callBackFunc != undefined && callBackFunc != null){
			callBackFunc();
		}
	}, null);	
}
function initPolluteGroupData(url,data, callBackFunc) {	
	ajaxRequest(url, null, data, null, function(resp) {
		initPolluteGroupHtml(resp.rows,callBackFunc);
		if (resp.rows.length >0) {
			$(".box-zsgc").css("height","auto");
			$(".factor").css("width",$(".n-dttitle").width()-20);
			
			$(".sl-e-more").html("展开<i></i>");
			$(".sl-e-more").removeClass("opened");
			if($(".box-zsgc").height()>37){
				$(".box-zsgc").css("height","37px");
				$(".sl-ext").show();
			}else{
				$(".sl-ext").hide();
			}						
		}
		if (callBackFunc != undefined && callBackFunc != null){
			callBackFunc();
		}
	}, null);	
}
function initNewPolluteGroupData(url,data, callBackFunc) {	
	ajaxRequest(url, null, data, null, function(resp) {
		initNewPolluteGroupHtml(resp.rows,callBackFunc);
		if (resp.rows.length >0) {
			$(".box-zsgc").css("height","auto");
			$(".factor").css("width",$(".n-dttitle").width()-20);
			
			$(".sl-e-more").html("展开<i></i>");
			$(".sl-e-more").removeClass("opened");
			if($(".box-zsgc").height()>37){
				$(".box-zsgc").css("height","37px");
				$(".sl-ext").show();
			}else{
				$(".sl-ext").hide();
			}						
		}
		if (callBackFunc != undefined && callBackFunc != null){
			callBackFunc();
		}
	}, null);	
}
function initPolluteHtmlForMultiStations(rows,callBackFunc) {
	var htmlVal = '<div class="factor"><div class="box-zsgc">';
	htmlVal += '<span class="fl marrt20" style="margin:3px 5px 4px 10px;"><b>因&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;子：</b></span>';
	htmlVal += '<div class="factors-div">';
	htmlVal += '<div id="polluteCodeDiv">';
	for(var i=0;i<rows.length;i++){
		if(count　< 6){
			htmlVal += '<span class="fl air-btn active factorbtn" polluteClass="'+rows[i].polluteClass+'" id="'+rows[i].polluteCode+'" >'+rows[i].polluteName+'</span>';
			count++;
		}else{
			htmlVal += '<span class="fl air-btn factorbtn" polluteClass="'+rows[i].polluteClass+'" id="'+rows[i].polluteCode+'" >'+rows[i].polluteName+'</span>';
		}
		
	}
	htmlVal += '</div></div></div>';	
	htmlVal += '<div class="sl-ext">';
	htmlVal += '<a class="n-cmbtn pd-jx btn-gray-border sl-e-more text-lf">展开<i></i></a>';
	htmlVal += '</div></div>'; 
	$("#factor").html(htmlVal);
	$(".factorbtn").bind("click",function(){
		checkSix($(this).attr("id"),callBackFunc);
	});
	$(".sl-ext").hide();
	$(".sl-ext").bind('click', function(event) {
		if ($(".sl-e-more").hasClass("opened")) {
			$(".sl-e-more").html("展开<i></i>");
			$(".sl-e-more").removeClass("opened");
			$(".box-zsgc").css("height","37px");
		} else {				
			$(".sl-e-more").html("收起<i></i>");
			$(".sl-e-more").addClass("opened");
			$(".box-zsgc").css("height","auto");
		}
	});
}
function checkSix(id,callBackFunc) {
	if ($("#" + id).hasClass('active')) {
		$("#" + id).removeClass('active');
		count--;
	} else {
		if(count < 6){
			$("#" + id).addClass('active');
			count++;
		}else{
			$.messager.alert('提示', '<br>选择的因子不能超过6个！<br>', 'info');
		}
	}
	if (callBackFunc != undefined && callBackFunc != null){
		callBackFunc();
	}
}
function initPolluteDataForMultiStations(url,data, callBackFunc) {	
	ajaxRequest(url, null, data, null, function(resp) {
		initPolluteHtmlForMultiStations(resp.rows,callBackFunc);
		if (resp.rows.length >0) {
			$(".box-zsgc").css("height","auto");
			$(".factor").css("width",$(".n-dttitle").width()-20);
			
			$(".sl-e-more").html("展开<i></i>");
			$(".sl-e-more").removeClass("opened");
			if($(".box-zsgc").height()>37){
				$(".box-zsgc").css("height","37px");
				$(".sl-ext").show();
			}else{
				$(".sl-ext").hide();
			}						
		}
		if (callBackFunc != undefined && callBackFunc != null){
			callBackFunc();
		}
	}, null);	
}